var express = require('express');
var router = express.Router();
var BaseData = require('./config/db');
db = new BaseData();
db.connection.connect();
 
/* GET home page. */
router.get('/', function(req, res, next) {
	if(req.session.token) {
		var Str = req.session.token;
		var User_id = db.Toolaes(Str.toString().split("_")[0], "Decrypt");
		res.render('index', {
			User_id: User_id,
			GetStringConfig: db.GetStringConfig
		});
	} else {
		res.render('index', {
			User_id: null,
			GetStringConfig: db.GetStringConfig
		});
	}
})

router.post('/', function(req, res, next) {
	var Sub_id = [],
		User_id = [],
		Content = [],
		SubCent = [],
		SubColle = [],
		UpNumber = [],
		BuryNumber = [],
		SubTime = [],
		UserImg = [],
		Name = [];
	db.connection.query('select sub.Sub_id,sub.User_id,sub.Content,sub.SubCent,sub.SubColle,sub.UpNumber,sub.BuryNumber,sub.SubTime,user.UserImg,user.Name from subinfo sub inner join userinfo user on sub.User_id=user.User_id where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(`SubTime`) and sub.ContentType = 1 and sub.Status = 2 ', function(err, rows) {
		if(err) {
			res.json({
				"code": "3002",
				"msg": "系统出现错误,请联系管理员"
			})
			return false;
		}
		if(rows.length > 0) {
			for(var i = 0; i < rows.length; i++) {
				Sub_id.push(rows[i]['Sub_id']);
				User_id.push(rows[i]['User_id']);
				Content.push(rows[i]['Content']);
				SubCent.push(rows[i]['SubCent']);
				SubColle.push(rows[i]['SubColle']);
				UpNumber.push(rows[i]['UpNumber']);
				BuryNumber.push(rows[i]['BuryNumber']);
				SubTime.push(rows[i]['SubTime']);
				UserImg.push(rows[i]['UserImg']);
				Name.push(rows[i]['Name']);
			}
			res.json({
				"code": "3000",
				"msg": "success",
				"Sub_id": Sub_id,
				"User_id": User_id,
				"Content": Content,
				"SubCent": SubCent,
				"SubColle": SubColle,
				"UpNumber": UpNumber,
				"BuryNumber": BuryNumber,
				"SubTime": SubTime,
				"UserImg": UserImg,
				"Name": Name
			})
		} else {
			res.json({
				"code": "3000",
				"msg": "没有更多数据"
			})
		}
	});
})

//获取内容详情
router.get('/:Sub_id_1/:Sub_id_2.html', function(req, res, next) {
	if(req.params.Sub_id_1 != req.params.Sub_id_2) {
		res.redirect('/');
	}
	db.connection.query('select sub.Sub_id,sub.User_id,sub.Content,sub.SubCent,sub.SubColle,sub.UpNumber,sub.BuryNumber,sub.SubTime,user.UserImg,user.Name from subinfo sub inner join userinfo user on sub.User_id=user.User_id where sub.Sub_id = ?', [req.params.Sub_id_1], function(err, rows) {
		if(err) {
			res.json({
				"code": "3002",
				"msg": "系统出现错误,请联系管理员"
			})
			return false;
		}
		if(rows.length) {
			res.send(rows);
		} else {
			res.json({
				"code": "3000",
				"msg": "没有更多数据"
			})
		}
	});
})

module.exports = router;